-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add rescue to support streamline error handling #7038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This implements inline `rescue` like those in Ruby [1], but more powerful in that users can specify the exception to be handled. The `rescue` method will be very convenient in scripting. [1] https://stackoverflow.com/questions/15396791/ruby-oneline-rescue
Without the change, exceptions other than the following will not be propagated. Instead, users will get `scala.MatchError`. op rescue { case ex: NullPointerException => 4 case ex: ArithmeticException => 3 }
However, with explict cases, user may match fatal errors except for non-local returns, which users should not catch.
// case ex: ReturnThrowable[_] => throw ex // bug #7041 | ||
case ex: E => | ||
// user should never match `ReturnThrowable`, which breaks semantics of non-local return | ||
if (fallback.isDefinedAt(ex) && !ex.isInstanceOf[ReturnThrowable[_]]) fallback(ex) else throw ex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe E
should b a subtype of Exception
to avoid catching other Throwable
s like ReturnThrowable
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is open to discussion --- this is the case where users specify handled exceptions explicitly, but indeed ReturnThrowable
should never be handled by user programs.
This implements inline
rescue
like those in Ruby [1], but more powerfulin that users can specify the exception to be handled.
The
rescue
method will be very convenient in scripting.[1] https://stackoverflow.com/questions/15396791/ruby-oneline-rescue
[2] https://bugs.ruby-lang.org/issues/6739